//
//  CYPersonReleaseVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/1/7.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYPersonReleaseVC.h"
#import "CYUser.h"
#import "CYURL.h"
#import "CYHttpTool.h"
#import "CYLoginHttpTool.h"
#import "CYJsonInfo.h"
#import "MJExtension.h"
#import "CYCreditTool.h"
#import "CYTool.h"
#import "TOWebViewController.h"
#import "LEBarButtonItem.h"
#import "FDSlideBar.h"
#import "CYPersonReModel.h"
#import "CYPersonReCell.h"
#import "CYPersonReDtVC.h"
#import "CYCommonReleaseTVC.h"
#import "CYJumpsTool.h"
#import "CYReleaceDetailVC.h"
#import "CYNEWFabuVC.h"
#import "CYWEITUOHelpVC.h"
#import "CYCuiShouCell.h"
#import "CYCuiShouModel.h"
#import "CYCSDetailVC.h"
#import "CYCSMoreVC.h"
#import "MBProgressHUD+LE.h"
#import "CYJumpsTool.h"
#import "CYShopViewController.h"


@interface CYPersonReleaseVC ()<UITableViewDataSource,UITableViewDelegate>
{
//    NSMutableArray *_cuishoudataArray;//数据源
    NSMutableArray *_putongdataArray;
//    FDSlideBar *_slideBar;
//    NSUInteger _slideBarSelectIndex;
    
}
@property (nonatomic, strong)UILabel *messlab;
@property (nonatomic, strong)UIView *lineview;
@property (nonatomic, strong)UILabel *nodatalab;
@property (nonatomic, strong)NSString *strid;
@property (nonatomic, strong)NSString *type;
@property (nonatomic, strong)UITableView *messageTabelView;
@property (nonatomic, strong)UILabel *laber;

@end

@implementation CYPersonReleaseVC

- (void)viewWillAppear:(BOOL)animated{
    self.extendedLayoutIncludesOpaqueBars = YES;
    [self downloaddata];
    [self setupNavigationView];
    [self.view addSubview:_laber];

}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"我的发布";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
//    _slideBar=[[FDSlideBar alloc]initWithFrame:CGRectMake(30, 64, [[ UIScreen mainScreen ] bounds].size.width-60, 40)];
//    [self.view addSubview:_slideBar];
//    _lineview = [[UIView alloc]initWithFrame:CGRectMake(0, _slideBar.y+_slideBar.height, [[ UIScreen mainScreen ] bounds].size.width, 1)];
    //CGFloat r = [[ UIScreen mainScreen ] bounds].size.width;
    _lineview.backgroundColor =  CYColor(100, 200, 100, 1);
    [self.view addSubview:_lineview];
//    _slideBar.itemsTitle = @[@"委托催收",@"不委托催收"];
//    _slideBar.itemColor= [UIColor grayColor];
//     _slideBar.sliderColor = CYColor(100, 200, 100,1);
//     _slideBar.itemSelectedColor =  CYColor(100, 200, 100, 1);
//    _slideBarSelectIndex = 0;
     UILabel *noticeLab = [[UILabel alloc] initWithFrame:CGRectMake(0,64, [[UIScreen mainScreen] bounds].size.width, self.view.height-64)];
     noticeLab.text = @"没有新的数据";
     noticeLab.textColor = [UIColor grayColor];
     noticeLab.textAlignment = NSTextAlignmentCenter;
     _laber = noticeLab;
//     [_slideBar slideBarItemSelectedCallback:^(NSUInteger idx) {
//        _slideBarSelectIndex = idx;
//         if (_slideBarSelectIndex == 0) {
//              if (!_cuishoudataArray.count) {
//                   _messageTabelView.hidden = YES;
//                   [self.view addSubview:noticeLab];
//              }
//              if (_cuishoudataArray.count) {
//                   [noticeLab removeFromSuperview];
//                   _messageTabelView.hidden = NO;
//              }
//              
//         }
//         else if (_slideBarSelectIndex == 1){
//              if (!_putongdataArray.count) {
//                   [self.view addSubview:noticeLab];
//                   _messageTabelView.hidden = YES;
//              }
//              if (_putongdataArray.count) {
//                   [noticeLab removeFromSuperview];
//                   _messageTabelView.hidden = NO;
//              }
//              
//         }
//        [_messageTabelView reloadData];
//    }];
//    _cuishoudataArray = [NSMutableArray array];
    _putongdataArray = [NSMutableArray array];
    _strid = [[NSString alloc]init];
    
     LEBarButtonItem *submit = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_fb"] andSelImage:nil andTitle:@"发布" target:self action:@selector(submitclick)];
     //     self.navigationItem.rightBarButtonItem = submit;
     LEBarButtonItem *help = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_bzpng"] andSelImage:nil andTitle:@"帮助" target:self action:@selector(helpclick)];
     
     
     [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: help,submit,nil]];

    [self cretetb];
    [self cretelb];
     

    // Do any additional setup after loading the view.
}


-(void)submitclick{
     CYNEWFabuVC *fabu = [[CYNEWFabuVC alloc]init];
     [self.navigationController pushViewController:fabu animated:YES];
     
}
-(void)helpclick{
     //
     CYWEITUOHelpVC *help  = [[CYWEITUOHelpVC alloc] init];
     [self.navigationController pushViewController:help animated:YES];
//     UIViewController *mainController = [[CYShopViewController alloc]init];
//     self.view.window.rootViewController = mainController;
     
//     CYCommonReleaseTVC  *bacd = [CYCommonReleaseTVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
//     [self.navigationController pushViewController:bacd animated:YES];
     
     
     
     
}


- (void)setupNavigationView {
    UIView *naviView = [[UIView alloc] init];
    naviView.backgroundColor = CYNaviThemeColor;
    naviView.frame = CGRectMake(0, 0, self.view.width, 64);
    [self.view addSubview:naviView];
}


-(void)downloaddata{
     
//     [_cuishoudataArray removeAllObjects];
     [_putongdataArray removeAllObjects];
    CYUser *user = [CYLoginHttpTool userData].user;
     
     
     if (user) {
          [MBProgressHUD showMessage:nil toView:self.view];
          [CYHttpTool postWithURL:[CYURL shared].getnetworkcollectrecordsbyUserid params:@{@"page":@"0",@"userId":user.userId,@"token":user.token} success:^(id json) {
               [MBProgressHUD hideHUDForView:self.view];
               CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
               if (jsonInfo.isSuccess) {
                    NSDictionary *data = jsonInfo.data;
                    NSArray *arr  =[data valueForKey:@"pageItems"];
                    if (arr.count) {
                         _laber.hidden = YES;
                         for (NSDictionary *dic in arr) {
                              CYPersonReModel *temp = [[CYPersonReModel alloc]initWithDictionary:dic];
                              _strid = (NSString *)[dic objectForKey:@"id"];
                              _type = (NSString *)[dic objectForKey:@"isExamine"];
                              [_putongdataArray addObject:temp];
                         }
                    }
               }
               [_messageTabelView reloadData];
          } failure:^(NSError *error) {
               [MBProgressHUD hideHUDForView:self.view];

               CYLog(@"error = %@",error);
          }];
          
//          [CYHttpTool postWithURL:[CYURL shared].getAppDefaulterList params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
//               [MBProgressHUD hideHUDForView:self.view];
//               CYJsonInfo *jsoninfo  = [CYJsonInfo objectWithKeyValues:json];
//               CYLog(@"jsoninfo = %@",jsoninfo);
//               if (jsoninfo.isSuccess) {
//                    NSArray *arr = jsoninfo.data;
//                    if (arr.count) {
//                         [_laber removeFromSuperview];
//                    }
//                    if (!arr.count) {
//                         [self.view addSubview:_laber];
//                    }
//                    if (arr.count) {
//                         for (NSDictionary *dic in arr) {
//                              CYCuiShouModel *temp = [[CYCuiShouModel alloc]initWithDictionary:dic];
//                              [_cuishoudataArray addObject:temp];
//                              
//                         }
//                    }
//                    
//                    
//               }
//               [_messageTabelView reloadData];
//               
//          } failure:^(NSError *error) {
//               CYLog(@"error = %@",error);
//               [MBProgressHUD hideHUDForView:self.view];
//               
//          }];

     }else{
          
          [CYJumpsTool jumpToLogin:self];
     }
     
     
     
     
//    [CYHttpTool postWithURL:[CYURL shared].entryui params:@{@"page":@"0",@"userId":userid,@"token":token} success:^(id json) {
//        CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
//        if (jsoninfo.isSuccess) {
//            NSDictionary *data = jsoninfo.data;
//            NSArray *arr = [data valueForKey:@"pageItems"];
//            if (arr) {
//                for (NSDictionary *dic in arr) {
//                    CYPersonReModel *temp = [[CYPersonReModel alloc]initWithDictionary:dic];
//                    _strid = (NSString *)[dic objectForKey:@"id"];
//                    _type = (NSString *)[dic objectForKey:@"isExamine"];
//                    [_dataArray1 addObject:temp];
//                }
//            }
//        }
//     [_messageTabelView reloadData];
//    } failure:^(NSError *error) {
//
//    }];
}

-(void)cretetb{
     
    _messageTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0,64, [[UIScreen mainScreen] bounds].size.width, self.view.height) style:UITableViewStylePlain];
    _messageTabelView.backgroundColor = [UIColor whiteColor];
    _messageTabelView.showsHorizontalScrollIndicator=NO;
    _messageTabelView.showsVerticalScrollIndicator=NO;
    self.messageTabelView.dataSource = self;
    self.messageTabelView.delegate = self;
//    [_messageTabelView registerNib:[UINib nibWithNibName:@"CYPersonReCell" bundle:nil] forCellReuseIdentifier:@"CYPersonReCell"];
//     if (_slideBarSelectIndex == 0) {
//          [_messageTabelView registerNib:[UINib nibWithNibName:@"CYCuiShouCell" bundle:nil] forCellReuseIdentifier:@"CYCuiShouCell"];
//     }
//     if (_slideBarSelectIndex == 0) {
          [_messageTabelView registerNib:[UINib nibWithNibName:@"CYPersonReCell" bundle:nil] forCellReuseIdentifier:@"CYPersonReCell"];
//     }
//    [_messageTabelView registerNib:[UINib nibWithNibName:@"CYCuiShouCell" bundle:nil] forCellReuseIdentifier:@"CYCuiShouCell"];
    [CYTool setExtraCellLineHiddenWithTableView:_messageTabelView];
    [self.view addSubview:_messageTabelView];
}
- (void)cretelb{
    //无数据提示view
    UILabel *label = [[UILabel alloc] init];
    label.text = @"您还没有发布任何违约信息！";
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:label];
    [label autoCenterInSuperview];
    self.nodatalab = label;
    label.hidden = YES;
    
}

#pragma mark - tableview代理
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

         //cell.model = _dataArray[indexPath.row];
//    if (_slideBarSelectIndex == 0) {
//        CYCuiShouCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYCuiShouCell" forIndexPath:indexPath];
//        cell.accessoryType = UITableViewCellAccessoryNone;
//        cell.model = _cuishoudataArray[indexPath.row];
//         return  cell;
//    }else if(_slideBarSelectIndex == 1){
         CYPersonReCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYPersonReCell" forIndexPath:indexPath];
     
         cell.model = _putongdataArray[indexPath.row];
         return cell;
//     }
     return nil;
//    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     
    return 90;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if (_slideBarSelectIndex == 0) {
//       
//         CYCSDetailVC *detail = [[CYCSDetailVC alloc]init];
//         detail.cycuishoumodel = [_putongdataArray objectAtIndex:indexPath.row];
//         CYCSMoreVC *more = [[CYCSMoreVC alloc]init];
//         more.cycuishoumodel = [_putongdataArray objectAtIndex:indexPath.row];
//         
//         [self.navigationController pushViewController:detail animated:YES];
//        
//    }
//    else if(_slideBarSelectIndex == 1){
         CYPersonReDtVC *persondt = [[CYPersonReDtVC alloc]init];
         persondt.cypermodel = [_putongdataArray objectAtIndex:indexPath.row];
         [self.navigationController pushViewController:persondt animated:YES];
//    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (_slideBarSelectIndex ==0) {
//        return _cuishoudataArray.count;
//    }
//    if (_slideBarSelectIndex == 1){
//    return _dataArray1.count;
//    }
    return _putongdataArray.count;
}
#pragma mark -
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation下

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
